package com.ecnu.sei.app.course;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ecnu.sei.app.resources.Classroom;
import com.ecnu.sei.app.resources.Time;
import com.ecnu.sei.app.teacher.AbstractTeacher;
import com.ecnu.sei.app.teacher.Official;
import com.ecnu.sei.app.util.Pair;
import com.ecnu.sei.app.util.Utils.CollegeYear;
import com.ecnu.sei.app.util.Utils.DayHour;

public class CourseInfo {
  private String courseId = null;
  private String className = null; // e.g. 0903 
  private String courseName = null;
  private CollegeYear collegeYear = CollegeYear.FRESHMAN;
  private AbstractTeacher teacher;  
  private List<Pair<Integer, DayHour>> courseTimeList = new ArrayList<Pair<Integer, DayHour>>();
  private int studentNum = 0;
  private List<Classroom> classroomList = new ArrayList<Classroom>();
  private ExamInfo examInfo = new ExamInfo();

  private final Logger LOG = LoggerFactory.getLogger(CourseInfo.class);

  public CourseInfo() {
  }

  public CourseInfo(String courseId, String courseName, String className,
      AbstractTeacher teacher, int dayOfWeek, DayHour dayHour,  
      int studentNum, Classroom classroom) {
    this.courseId = courseId;
    this.courseName = courseName;
    this.className = className;
    this.teacher = teacher;

    Pair<Integer, DayHour> courseTime = new Pair<Integer, DayHour>(dayOfWeek, dayHour);
    this.courseTimeList.add(courseTime);
    this.studentNum = studentNum;
    this.classroomList.add(classroom); 
  }

  public CourseInfo(String courseId, String courseName, String className,
      AbstractTeacher teacher, Pair<Integer, DayHour> courseTime,  
      int studentNum, Classroom classroom) {
    this.courseId = courseId;
    this.courseName = courseName;
    this.className = className;
    this.teacher = teacher;
    this.courseTimeList.add(courseTime);
    this.studentNum = studentNum;
    this.classroomList.add(classroom);
  }


  public String getCourseId() {
    return courseId;
  }


  public String getCourseName() {
    return courseName;
  }

  public String getClassName() {
    return className;
  }


  public CollegeYear getCollegeYear() {
    return collegeYear;
  }



  public AbstractTeacher getTeacher() {
    return teacher;
  }


  public List<Pair<Integer, DayHour>> getCourseTimeList() {
    return courseTimeList;
  }



  public int getStudentNum() {
    return studentNum;
  }


  // return the first classroom by default
  public Classroom getClassroom() {
    return classroomList.get(0);
  }



  public ExamInfo getExamInfo() {
    return examInfo;
  }



  public Logger getLOG() {
    return LOG;
  }



  public CourseInfo setCourseId(String courseId) {
    this.courseId = courseId;
    return this;
  }



  public CourseInfo setCourseName(String courseName) {
    this.courseName = courseName;
    return this;
  }



  public CourseInfo setCollegeYear(CollegeYear collegeYear) {
    this.collegeYear = collegeYear;
    return this;
  }



  public CourseInfo setTeacher(AbstractTeacher teacher) {
    this.teacher = teacher;
    return this;
  }


  public CourseInfo setClassName(String className) {
    this.className = className;
    return this;
  }

  public CourseInfo addCourseTime(Pair<Integer, DayHour> courseTime) {
    this.courseTimeList.add(courseTime);
    return this;
  }
  public CourseInfo addCourseTime(int dayOfWeek, DayHour dayHour) {
    return addCourseTime(new Pair<Integer, DayHour>(dayOfWeek, dayHour));
  }

  public CourseInfo setStudentNum(int studentNum) {
    this.studentNum = studentNum;
    return this;
  }


  public CourseInfo addClassroom(Classroom classroom) {
    this.classroomList.add(classroom);
    return this;
  }



  public CourseInfo setExamInfo(ExamInfo examInfo) {
    this.examInfo = examInfo;
    return this;
  }



  public void printExamInfo() {
    if (null == examInfo) {
      LOG.debug("exam info not available");
      return;
    }

    System.out.println("Course: " + courseName);
    System.out.println("Proctors: ");


    for (AbstractTeacher proctor : examInfo.getProctor()) {
      if (null == proctor) {
        LOG.debug("proctor not available");
        return;
      }
      System.out.println("\t" + proctor.getName() + ":" + proctor.getClass().getSimpleName());
    }
    Time time = examInfo.getTime();
    if (null == time) {
      LOG.debug("exam time not avaiable");
    } else {
      System.out.println("Time: " + time.getDate());
    }

    Classroom classroom = examInfo.getClassroom();
    if (null == classroom) {
      LOG.debug("classroom info not avaiable");
    } else {
      System.out.println("Classroom: " + classroom.getName());
    }
    Official patrol = examInfo.getPatrol();
    if (null == patrol) {
      LOG.debug("patrol info not available");
    } else {
      System.out.println("Patrol: " + patrol.getName());
    }
    Set<AbstractTeacher> backup = examInfo.getBackup();
    if (null == backup || 0 == backup.size()) {
      LOG.debug("backup info not available");
    } else {
      for (AbstractTeacher teacher : backup) {
        System.out.println("Backup: " + teacher.getName());
      }
    }
  }

  @Override
  public String toString() {
    return "CourseInfo [className=" + className + ", courseName=" + courseName
        + ", teacher=" + teacher.getName() + ", examInfo=" + examInfo + "]";
  }






}
